package cn.jdy.plugins.service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.RedPacketReceive;
import cn.jdy.plugins.entity.SecretKeyl;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jga
 * @title : 秘钥操作实现
 */
@Service
public class SecretKeylService extends BaseDao<SecretKeyl> {



	/**
	 * 按条件查询
	 * 
	 * @param RedPacketReceive
	 * @return
	 */
	@Cacheable(value = "cache1", key = "'help_id' + #RedPacketReceive.id")
	public List<SecretKeyl> query(SecretKeyl obj,String... date) {
		if (obj == null)
			return super.findAll();
		List<SecretKeyl> list = null;

		if (obj.getiDisplayLength() > 0) {
			list = super.find(createFilter(obj, date), obj.getiDisplayStart(),
					obj.getiDisplayLength());
		} else {
			list = super.find(createFilter(obj, date));
		}

		return list;
	}

	@Cacheable(value = "cache1", key = "'help_count' + #RedPacketReceive.id")
	public Long count(SecretKeyl RedPacketReceive) {
		return super.count(createFilter(RedPacketReceive));
	}

	/**
	 * 拼装条件
	 * 
	 * @param RedPacketReceive
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(SecretKeyl redPacketReceive, String... date) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if (null != redPacketReceive.getLiveId()) {
			dc.add(Restrictions.eq("liveId", redPacketReceive.getLiveId()));
		}
		if (!StringUtil.isNullOrEmpty(redPacketReceive.getUserOpenId())) {
			dc.add(Restrictions.eq("userOpenId", redPacketReceive.getUserOpenId()));
		}
		if (!StringUtil.isNullOrEmpty(redPacketReceive.getUserNickName())) {
			dc.add(Restrictions.like("userNickName", redPacketReceive.getUserNickName(),MatchMode.ANYWHERE));
		}
		if (date.length > 0 && !StringUtil.isNullOrEmpty(date[0])
				&& !StringUtil.isNullOrEmpty(date[1])) {
			dc.add(Restrictions.between("createTime", date[0], date[1]));
		}
		dc.addOrder(Order.desc("createTime"));
		return dc;
	}



}
